package edu.dali.enterprise_test.model.vo;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by hufa on 2017-01-31.
 */
public class Dto extends HashMap<String , Object> {
    /**
     * 获取字符串
     *
     * @param field 需获取的字段
     * @return
     */
    public String getAsString(String field) {
        String ret = "";
        Object o = this.get(field);
        if (o != null) ret = o.toString();
        return ret;
    }

    /**
     * 获取整型
     *
     * @param field 需获取的字段
     * @return
     */
    public Integer getAsInt(String field) {
        Integer ret = null;
        Object o = this.get(field);
        if (o != null) ret = Integer.parseInt(o.toString());
        return ret;
    }

    /**
     * 获取长整型
     *
     * @param field 需获取的字段
     * @return
     */
    public Long getAsLong(String field) {
        Long ret = null;
        Object o = this.get(field);
        if (o != null) ret = Long.parseLong(o.toString());
        return ret;
    }

    /**
     * 获取大小数
     *
     * @param field 需获取的字段
     * @return
     */
    public BigDecimal getAsBigDecimal(String field) {
        BigDecimal ret = new BigDecimal(0);
        Object o = this.get(field);
        if (o != null && !o.toString().equals("")) ret = new BigDecimal(o.toString());
        return ret;
    }

    /**
     * 获取小数
     *
     * @param field 需获取的字段
     * @return
     */
    public Double getAsDouble(String field) {
        Double ret = new Double(0);
        Object o = this.get(field);
        if (o != null) ret = Double.parseDouble(o.toString());
        return ret;
    }

    /**
     * 获取时间
     *
     * @param field   需获取的字段
     * @param partten 格式化类型
     * @return
     */
    public Date getAsDate(String field, String partten) {
        Date ret = null;
        SimpleDateFormat sdf = new SimpleDateFormat(partten);
        Object o = this.get(field);
        if (o != null) try {
            ret = sdf.parse(o.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 获取时间
     *
     * @param field 需获取的字段
     * @return
     */
    public Date getAsDate(String field) {
        return this.getAsDate(field, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取时间字符串
     *
     * @param field   需获取的字段
     * @param partten 时间格式
     * @return
     */
    public String getAsDateString(String field, String partten) {
        String ret = "";
        SimpleDateFormat sdf = new SimpleDateFormat(partten);
        Object o = this.get(field);
        if (o != null) ret = sdf.format(o);
        return ret;
    }

    /**
     * 获取时间字符串
     *
     * @param field 需获取的字段
     * @return
     */
    public String getAsDateString(String field) {
        return getAsDateString(field, "yyyy-MM-dd HH:mm:ss");
    }

    public Boolean getAsBoolean(String field) {
        Object o = this.get(field);
        if (o instanceof Boolean) {
            return (Boolean) o;
        }
        return null;
    }
}
